<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
// import Joomla view library
jimport('joomla.application.component.view');
 

class ShopViewProcess extends JView
{
	function display($tpl = null) 
	{
	$mainframe = &JFactory::getApplication();
	$db = JFactory::getDBO();
	JRequest::setVar('format', 'raw');
	
	JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR . '/tables');
	$row = JTable::getInstance('Attach','ShopTable');
	
	$type = JRequest::getVar('type');
	$id = JRequest::getVar('id',0);
	switch($type)
	{
		case "upload":
			include  JPATH_COMPONENT_ADMINISTRATOR . DS . "class.upload.php";
			$img_path = JPATH_SITE . DS . 'images' . DS . 'image_product' .DS;
			//-----------
				$img = new Upload($_FILES['uploadfile']);
				$oldname = $_FILES['uploadfile']['name'];
				$filename =  $this->name_id_element(JFilterOutput::stringURLSafe($oldname)).'_'.time();
				
				// begin upload 
				if ($img->uploaded) {	  					
				//--- upload image origin
				$img->image_convert = 'jpg';
				$img->file_new_name_body = $filename;
				$img->Process($img_path);
				
				// resize image thumb
				$img->file_new_name_body = $filename;
				$img->image_resize = true;
				$img->image_convert = 'jpg';
				$img->image_x = 160;
				$img->image_ratio_y = true;
				$img->Process($img_path.'thumb'.DS);
				if ($img->processed) {
				
				$row->product_id = $id;
				$row->oldname = $oldname;
				$row->filename = $filename.'.jpg';
				$row->ext = 'jpg';
				if($row->store())
					{
					$pathimage = JURI::root().'images/image_product/thumb/';
						$str = '<li id="'.$row->id.'">
								<img src="'.$pathimage.$row->filename.'" width="50" height="50" /><br />
								<input type="radio" checked="checked" name="mainimage" value="'.$row->filename.'" class="mainimage" id="mainimage" />
								<label>Ảnh chính</label><br />
								<input type="hidden" name="imageattach[]" value="'.$row->id.'" />
								<a href="javascript:;" onclick="removefile('.$row->id.');">Xóa ảnh</a>
							</li>';
						echo 'ok=='.$row->id.'=='.$oldname.'=='.$str;
					}
					else
					{
						echo 'error=='.$oldname;
					}
				}
			}
				// end upload
		break;
		case "deletefile":
			$id = JRequest::getVar('id');
			$row->load($id);
			$img_path = JPATH_SITE . DS . 'images' . DS . 'image_product' .DS;
			$this->deleteFile($img_path.'thumb'.DS,$row->filename);
			$this->deleteFile($img_path,$row->filename);
			$row->delete();
			echo $id;
		break;
	}
	 $mainframe->close();
	 //end display
	}
	function deleteFile($path, $filename){
		$upfile = $path . $filename;
		if (file_exists($upfile)){
			unlink($upfile);
		}
	}
	/**
	* function get element extention name for image
	*/
	function name_id_element($string = null) {
    	$string = trim($string);
    	$string = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", 'a', $string);
    	$string = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", 'e', $string);
    	$string = preg_replace("/(ì|í|ị|ỉ|ĩ)/", 'i', $string);
    	$string = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", 'o', $string);
    	$string = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", 'u', $string);
    	$string = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", 'y', $string);
    	$string = preg_replace("/(đ)/", 'd', $string);

    	$string = preg_replace("/(À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ)/", 'A', $string);
    	$string = preg_replace("/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/", 'E', $string);
    	$string = preg_replace("/(Ì|Í|Ị|Ỉ|Ĩ)/", 'I', $string);
    	$string = preg_replace("/(Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ)/", 'O', $string);
    	$string = preg_replace("/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", 'U', $string);
    	$string = preg_replace("/(\'|\")/", '', $string);
    	$string = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", 'Y', $string);
    	$string = preg_replace("/(Đ)/", 'D', $string);
    	$string = str_replace("--", '_', $string);
    	$string = str_replace("--", '_', $string);
    	$string = str_replace("--", '_', $string);
        $string = str_replace("-", '_', $string);

        $string = str_replace(" ", '_', $string);
         $string = str_replace("/", '_', $string);
    	return $string;
    }


	
}